package com.openbravo.data.loader.ui;

import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.BoxLayout;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.ListQBFModelNumber;
import com.openbravo.data.loader.LocalRes;
import e.odbo.data.dsl.query.QBFCompareEnum;
import e.odbo.data.dsl.query.QBFParameter;
import e.odbo.data.dsl.query.QBFParameters;
import e.odbo.data.dao.table.Field;

import java.awt.Dimension;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.BevelBorder;
import java.awt.Rectangle;

public class JQBFParameterPanel extends JPanel implements ItemListener {
	private JTextField jTxtValue;
	private Field[] fields;
	ComboBoxValModel fieldNamesValModel,linkValModel;
	ListQBFModelNumber opModel;
	private JComboBox opComboBox;
	private JComboBox fieldName;
	private JComboBox linkComboBox;
	private JPanel panel;
	private JPanel panel_1;

	/**
	 * Create the panel.
	 */
	public JQBFParameterPanel() {
		setBounds(new Rectangle(0, 0, 0, 1));
		setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		setMaximumSize(new Dimension(32767, 50));
		setPreferredSize(new Dimension(260, 60));
		initComponents();
		fieldNamesValModel=new ComboBoxValModel();
		linkValModel=new ComboBoxValModel();
		linkValModel.add(new ComboBoxValEntry(QBFParameters.LINKTYPE_And,LocalRes.getIntString("QBFParameters.LINKTYPE_And")));
		linkValModel.add(new ComboBoxValEntry(QBFParameters.LINKTYPE_Or,LocalRes.getIntString("QBFParameters.LINKTYPE_Or")));

		fieldName.setModel(fieldNamesValModel);
		linkComboBox.setModel(linkValModel);
		linkValModel.setSelectedFirst();
		opComboBox.setRenderer(new QBFCompareEnumListCellRenderer());
	}
	
	
	private void initComponents() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		panel = new JPanel();
		add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		fieldName = new JComboBox();
		panel.add(fieldName);
		fieldName.addItemListener(this);
		fieldName.setPreferredSize(new Dimension(80, 25));
		
		JButton btnNewButton = new JButton("");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resert();
			}
		});
		
		opComboBox = new JComboBox();
		panel.add(opComboBox);
		opComboBox.setPreferredSize(new Dimension(55, 21));
		panel.add(btnNewButton);
		btnNewButton.setPreferredSize(new Dimension(20, 9));
		btnNewButton.setIcon(new ImageIcon(JQBFParameterPanel.class.getResource("/org/jdesktop/swingx/plaf/basic/resources/error16.png")));
		
		panel_1 = new JPanel();
		add(panel_1);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
		
		jTxtValue = new JTextField();
		panel_1.add(jTxtValue);
		jTxtValue.setColumns(10);
		
		
		linkComboBox = new JComboBox();
		panel_1.add(linkComboBox);
		linkComboBox.setPreferredSize(new Dimension(45, 25));
	}

	public QBFParameter getQBFParameter(){
		QBFParameter para=null;
		Field field=(Field) fieldNamesValModel.getSelectedItem();
		if(field!=null&&opComboBox.getSelectedItem()!=QBFCompareEnum.COMP_NONE){
			para=new QBFParameter(field.getLabel());
			para.setQbf((QBFCompareEnum) opComboBox.getSelectedItem());
			try {
				para.setValue(field.getFormat().parseValue(jTxtValue.getText()));
			} catch (BasicException e) {
				para=null;
			}
		}	
		return para;
	}
	
	public int getLinkType(){
		return (Integer) linkValModel.getSelectedKey();
	}
	
	public void setFields(Field[] fields){
		this.fields=fields;
		ArrayList list=new ArrayList();
		if(fields!=null){
		    list.add(0, null);
		    for(Field f:fields)
			    list.add(f);
		}
		fieldNamesValModel.refresh(list);
	}
	
	public void resert() {
		fieldNamesValModel.setSelectedItem(null);
		opModel=ListQBFModelNumber.getMandatoryNull();
	    opComboBox.setModel(opModel);
		jTxtValue.setText(null);
		linkValModel.setSelectedFirst();
	}
	@Override
	public void itemStateChanged(ItemEvent e) {
		Field field=(Field) e.getItem();
		if(field!=null){
			opModel=ListQBFModelNumber.getQBFModel(field.getData());
		}else{
			opModel=ListQBFModelNumber.getMandatoryNull();
		}
		opComboBox.setModel(opModel);
	}
}
